package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.access.EntityMjWhiteInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoWhiteInfo;
import com.ocom.srvpaccess.service.WhiteInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class WhiteInfoServiceImpl extends ServiceImpl<IDaoWhiteInfo, EntityMjWhiteInfo> implements WhiteInfoService {


    @Autowired
    private IDaoWhiteInfo daoWhiteInfo;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjWhiteInfo selectMaxId(Long companyId, Long areaId) {
        QueryWrapper<EntityMjWhiteInfo> query = Wrappers.query();
        query.eq("com_id",companyId).eq("area_id",areaId).eq("state",1)
                .orderByDesc("id").last("limit 1");

        return this.getOne(query);


    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjWhiteInfo> selectDeleteWhiteInfo(Long companyId, Long areaId, Long listDelete, Integer listLimit) {
        QueryWrapper<EntityMjWhiteInfo> query = Wrappers.query();
        query.eq("com_id",companyId).eq("area_id",areaId).gt("id",listDelete).eq("state",1);
        if (listLimit > 0){
            query.orderByAsc("id").last("limit "+listLimit);
        }else {
            query.orderByAsc("id").last("limit 10");
        }
        return daoWhiteInfo.selectList(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjWhiteInfo> selectAddWhiteInfo(Long companyId, Long areaId, Long listAdd, Integer listLimit) {
        QueryWrapper<EntityMjWhiteInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("area_id",areaId);
        query.and(id -> id.gt("id",listAdd));
        query.and(state -> state.eq("state",0));

        if ( listLimit > 0){
            query.orderByAsc("id").last("limit "+listLimit);
        }else {
            query.orderByAsc("id").last("limit 10");
        }
        return daoWhiteInfo.selectList(query);
    }
    //从门禁白名单mj_white_info再查找一次有没有新的白名单：
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjWhiteInfo> selectWhiteInfo(Long companyId, Long areaId, Long listAdd, Long listDelete) {

        QueryWrapper<EntityMjWhiteInfo> whiteInfoQueryWrapper=new QueryWrapper<>();
        whiteInfoQueryWrapper.eq("com_id",companyId);
        whiteInfoQueryWrapper.eq("area_id",areaId);
        whiteInfoQueryWrapper.and(wrapper ->{
            wrapper.and(itemWrapper ->{
                    itemWrapper.gt("id",listAdd);
                    itemWrapper.eq("state",0);
                });
            wrapper.or(itemWrapper -> {
                itemWrapper.gt("id", listDelete);
                itemWrapper.eq("state", 1);
            });
        });
        whiteInfoQueryWrapper.orderByAsc("id");
       return daoWhiteInfo.selectList(whiteInfoQueryWrapper);
    }
}
